package com.kingwood.algorithm.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MathExpCalc {

    public static int getResult(String str) {
        int len = str.length();
        if (len <= 0) {
            return 0;
        }

        String opStr = "+-*";
        List<String> list = new ArrayList<>();
        int start = -1; // 合法表达式开始的位置
        char prefix = 'a'; // 记录上一个字符

        for (int i=0; i<len; i++) {
            char cur = str.charAt(i);
            if (cur >= '0' && cur <= '9') {
                if (prefix == 'a') {
                    prefix = cur;
                    if (start < 0) {
                        start = i;
                    }
                    continue;
                }

                // 0000+00+200
                // 首字符是0，下一个必须是操作符
                if (prefix == '0') {
                    // 非法，结算
                    if (start >= 0 && (i - start) > 2) {
                        list.add(str.substring(start, i));
                    }
                    start = i;
                    prefix = cur;
                }
            } else if (opStr.contains(String.valueOf(cur))) {
                if (i < len - 1) {
                    char next = str.charAt(i+1);
                    // 操作符的下一个必须是数字
                    if (next < '0' || next > '9') {
                        // 结算
                        if (start >= 0 && (i - start) > 2) {
                            list.add(str.substring(start, i));
                        }
                        start = -1;
                    }
                } else {
                    // 最后一个是操作符号
                    // 结算
                    if (start >= 0 && (i - start) > 2) {
                        list.add(str.substring(start, i));
                    }
                    start = -1;
                }
                prefix = 'a';
            } else {
                // 结算
                if (start >= 0 && (i - start) > 2) {
                    list.add(str.substring(start, i));
                }
                start = -1;
                prefix = 'a';
            }
        }

        // 最后一个位置是数字
        // 结束，结算
        if (start >= 0) {
            list.add(str.substring(start));
        }

        int max = Integer.MIN_VALUE;
        String calStr = "";
        for (String item : list) {
            if (item.contains("+") || item.contains("-") || item.contains("*") || item.contains("/")) {
                System.out.println(item);
                if (item.length() > max) {
                    max = item.length();
                    calStr = item;
                }
            }
        }

        System.out.println("最长的表达式是：");
        System.out.println(calStr);
        return compute(calStr);
    }

    public static int compute(String str) {
        if (str.length() <= 0) {
            return 0;
        }

        str += "+0"; // 加上最后这一个避免遗漏最后一个数字的计算
        char[] charArr = str.toCharArray();
        int len = charArr.length;
        ArrayList<Integer> numbers = new ArrayList<>();
        ArrayList<Character> ops = new ArrayList<>();

        int cur = 0;
        for (int i=0; i<len; i++) {
            if (charArr[i] >= '0' && charArr[i] <= '9') {
                cur = cur * 10 + charArr[i] - '0';
            } else {
                int n = numbers.size();
                if (n == 0 || ops.get(n - 1) == '+' || ops.get(n - 1) == '-') {
                    numbers.add(cur);
                    ops.add(charArr[i]);
                } else {
                    int topNumber = numbers.get(n - 1);
                    char topOp = ops.get(n - 1);

                    if (topOp == '*') {
                        numbers.set(n - 1, topNumber * cur);
                    } else {
                        numbers.set(n - 1, topNumber / cur);
                    }

                    ops.set(n - 1, charArr[i]);
                }
                cur = 0;
            }
        }

        int n = numbers.size();
        int ans = numbers.get(0);
        for (int i = 1; i < n; i++) {
            ans += ops.get(i - 1) == '+' ? numbers.get(i) : -numbers.get(i);
        }

        return ans;
    }

    //判断字符串是否只有数字
    private static boolean isNumber(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    //利用递归方法计算四则运算
    private static int calculate(String str) {

        if (isNumber(str)) {
            return Integer.parseInt(str);
        }

        if (str.contains("+")) {
            int index = str.indexOf("+");
            return calculate(str.substring(0, index)) + calculate(str.substring(index + 1));
        }

        if (str.contains("-")) {
            int index = str.indexOf("-");
            return calculate(str.substring(0, index)) - calculate(str.substring(index + 1));
        }

        if (str.contains("*")) {
            int index = str.indexOf("*");
            return calculate(str.substring(0, index)) * calculate(str.substring(index + 1));
        }

        if (str.contains("/")) {
            int index = str.indexOf("/");
            return calculate(str.substring(0, index)) / calculate(str.substring(index + 1));
        }
        return 0;
    }

    //借助正则表达式拆出符合要求的表达式
    private static String dealWith(String str) {

        //将除数等于0替换掉,注意string是不可变量，所以要用新的变量代替
        String replaceStr = str.replace("/0", "a0");

        int max = 0;
        String longStr = "";
        String regex = "\\d+([\\+\\-*/]\\d+)+";

        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(replaceStr);

        while (matcher.find()) {
            String group = matcher.group();
            System.out.println(group);
            if (group.length() > max) {
                max = group.length();
                longStr = group;
            }

        }
        return longStr;
    }

    public static void main(String[] args) {
        //012+a-5+200-123*10+c+10-01+9-8*5
        //012+a-5+02-12*10+c+10-01+9-8*5
        //aaavvvdddeee4-4+6fff333+5+9+ 12 -1000++++++5555_5555avg444+5h90hhh1+2abc

        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        //将空格替换掉，将除数等于0替换掉
        String replaceStr = str.replaceAll(" ", "").replaceAll("/0", "a0");
        System.out.println(replaceStr);
        // 5+200-123*10
        // 两种解法我的解法更好
        System.out.println(getResult(str));
        String longestReg = dealWith(str);
        System.out.println(longestReg);
        System.out.println(calculate(longestReg));
    }
}
